from fastapi import APIRouter, Depends, Request, Query
from sqlalchemy.orm import Session

from server.controller.check_token import get_current_user
from server.controller.user import (
    create_user, get_info, get_user_list, update_user_info,
    delete_user_for_id, reset_user_password
)
from server.dependencies.check_permission import check_permission
from server.etc.database import create_db
from server.schemas.comment import WriteResponse, SystemErrors, ArgError, NotExistError
from server.schemas.user import (
    UserCreate, UserUpdate, UserDelete,
    UserPasswordReset, UserAlreadyExists, UserQueryResponse,
    UserListResponse, PasswordValidateError
)
from utils.paginatio import PaginationParams

router = APIRouter()


@router.post(
    "/adduser",
    response_model=WriteResponse,
    responses={
        400: {"model": UserAlreadyExists, "description": "用户已存在"},
        402: {"model": PasswordValidateError, "description": "用户密码复杂度不满足要求"},
        500: {"model": SystemErrors, "description": "系统错误"}
    },
)
async def adduser(
        request: Request,
        user: UserCreate,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("user:manage"))

):
    """
    添加用户
    """
    result = create_user(request, user, db, current_user)
    return result


@router.get(
    "/getuserinfo",
    response_model=UserQueryResponse,
    responses={
        404: {"model": NotExistError, "description": "用户不存在"},
        500: {"model": SystemErrors, "description": "系统错误"},
        503: {"model": ArgError, "description": "参数错误"},
    }
)
async def get_user_info(
        request: Request,
        userid: str | None = Query(default=None,  description="用户ID"),
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("user:manage"))
):
    """
    获取用户信息
    """
    result = get_info(request, userid, db, current_user)
    return result


@router.post(
    "/getuserlist",
    response_model=UserListResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def get_users(
        request: Request,
        page: PaginationParams = PaginationParams(),
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("user:manage"))
):
    """
    获取用户列表
    """
    result = get_user_list(request, page, db, current_user)
    return result


@router.post(
    "/updateuserinfo",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def update_user(
        request:Request,
        user:UserUpdate,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("user:manage"))
):
    """
    修改用户信息
    """
    result = update_user_info(request, user, db, current_user)
    return result


@router.post(
    "/deleteuser",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def delete_user(
        request: Request,
        userid: UserDelete,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("user:manage"))
):
    """
    删除用户
    """
    result = delete_user_for_id(request, userid, db, current_user)
    return result


@router.post(
    "/resetpassword",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def reset_password(
        request: Request,
        userid: UserPasswordReset,
        db: Session = Depends(create_db),
        current_user: str = Depends(get_current_user)
):
    """
    密码重置
    """
    result = reset_user_password(request, userid, db, current_user)
    return result


